﻿#ifndef USERLOGIN_H
#define USERLOGIN_H
#pragma execution_character_set("utf-8")
#include <qdialog.h>
#include <QWidget>
#include <QEventLoop>
#include <QMouseEvent>
#include <algorithm>
#include <map>
#include <memory>
#include <thread>
#include <mutex>
#include <fstream>
#include <QLineEdit>
#include <memory>
#include <socket/sockclient.h>
#include "sqldb.h"
#include "chessshow.h"
namespace Ui {
class UserLogin;
}


class UserLogin : public QDialog
{
    Q_OBJECT
public:
    ~UserLogin();
    static UserLogin* getInstance() {
        static std::once_flag flag;
        std::call_once(flag,[=](){_instance=new UserLogin();});
        return _instance;
    }
    enum {Loading, Registing, Free};
    int result()const {
        return m_result;
    }
    QString getLimited() {
        return m_limited;
    }
    QString getUserName() {
        return userName;
    }
public:
    void loadSuccess  ();
    void loadFail     ();
    void registSuccess();
    void registFail   ();
    void setSocketClient(shared_ptr<SockClient> tcp_com);
    void setGame(ChessShow *game);
signals:
    void s_loadSuccess();
    void s_loadFail     ();
    void s_registSuccess();
    void s_registFail   ();
protected:
    bool eventFilter  (QObject *obj, QEvent *evt);
    void closeEvent ( QCloseEvent* e);
public slots:
    void login();
    void QuitAndExit();
    void saveUserInfo(QString count,QString password);
private:
    explicit UserLogin(QDialog* parent = 0);
    UserLogin(const UserLogin& obj) = delete;
    UserLogin& operator=(const UserLogin& obj) = delete;
    UserLogin(const UserLogin&& obj) = delete;
    UserLogin& operator=(const UserLogin&& obj) = delete;
private:
    void       initUserInfo();
    void       paintEvent(QPaintEvent *);
    bool       checkString(QString check_info);
    void       RegistBtn_clicked();
private slots:
    void       on_comboBox_count_activated(const QString &arg1);

public slots:
    void       clearCount();
    QPixmap    PixmapToRound(QPixmap &src, int radius);
    void       loadPic      (std::string pic_path);
private:
    Ui::UserLogin*         ui;
    static UserLogin*      _instance;
    int                    m_result;
    QString                m_limited;
    QString                userName;
    int                    tryCount;
    std::vector<UserInfo>  m_vec;
    QLineEdit             *lineEdit;
    shared_ptr<SockClient> sock_client;
    int                    m_status=Free;
    QString                m_count;
    QString                m_pasw;
    ChessShow              *m_game=nullptr;
};

#endif // USERLOGIN_H
